package com.molyfun.parents.view;

import android.content.Context;
import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.molyfun.parents.R;


/**
 * Created by liuchao on 8/18/16.
 * cell间距
 */
public class ItemCellDecoration extends RecyclerView.ItemDecoration{

    private static final int COMMON_COLUMN_COUNT = 3;

    private int mHoriSpace;
    private int mVerticalSpace;
    private int mColumnsCount;

    private int mDoubleHoriSpace;
    private int mOneHalfHoriSpace, mOneHalfVerticalSpace;

    public ItemCellDecoration(Context context) {
        this(context, COMMON_COLUMN_COUNT);
    }


    public ItemCellDecoration(Context context, int columnsCount) {
        this.mHoriSpace = context.getResources().getDimensionPixelSize(R.dimen.common_cell_hori_space);
        this.mVerticalSpace = context.getResources().getDimensionPixelSize(R.dimen.common_cell_vertical_space);
        mDoubleHoriSpace = mHoriSpace * 2;
        mOneHalfHoriSpace = mHoriSpace + mHoriSpace / 2;
        mOneHalfVerticalSpace = mVerticalSpace + mVerticalSpace / 2;
        this.mColumnsCount = columnsCount;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        if(parent.getChildLayoutPosition(view)/mColumnsCount < 1){
            outRect.top = mOneHalfVerticalSpace;
        }else{
            outRect.top = mVerticalSpace;
        }
        if(mColumnsCount != 1){
            //最左边
            if(parent.getChildLayoutPosition(view)%mColumnsCount == 0){
                outRect.left = mDoubleHoriSpace;
                outRect.right = mHoriSpace;
                //最右边
            }else if(parent.getChildLayoutPosition(view)%mColumnsCount == (mColumnsCount - 1)){
                outRect.right = mDoubleHoriSpace;
                outRect.left = mHoriSpace;
            }else{
                outRect.left = mOneHalfHoriSpace;
                outRect.right = mOneHalfHoriSpace;
            }
        }else{
            outRect.left = mDoubleHoriSpace;
            outRect.right = mDoubleHoriSpace;
        }

        outRect.bottom = mVerticalSpace;
    }
}